****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings
****************************************************************************
	
clear all
set more off

global lotcons "female black white hispanic lunch?? lunch_miss app2 app3 anyt109 anyt110 anyt111 anyt112 anyt1lunch?? esly??"  

global nccons "math_lag read_lag science_lag math_gr_lag read_gr_lag mardgr_int_lag science_gr_lag allgr_int_lag i.lowread_nonmag#i.year lep_Y_lag ma_aig_lag rd_aig_lag aig_int_lag"

use schlcode_avgchars_cms, clear

keep mathstand readstand schlcode

rename mathstand mathstand_hs 
rename readstand readstand_hs
rename schlcode homeschl

tempfile hsscores 
	save `hsscores'
		
use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & ksample & stayer==1)

merge m:1 homeschl using `hsscores' , gen(mergehsscores)
keep if (mergehsscores==1 | mergehsscores==3)

gen mathstand_diff = mathstand_at - mathstand_as
gen readstand_diff = readstand_at - readstand_as

gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as
gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)
gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier
gen move1_win_noncomp = win_noncomplier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1	

global outcomecats "move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp"
	
egen tercma = xtile(mathstand_hs) , n(3)
gen lowhsma = (tercma==1)
gen medhsma = (tercma==2)
gen highhsma = (tercma==3)

egen tercrd = xtile(readstand_hs) , n(3)
gen lowhsrd = (tercrd==1)
gen medhsrd = (tercrd==2)
gen highhsrd = (tercrd==3)

gen avgscores_hs = (0.5*mathstand_hs + 0.5*readstand_hs)
egen tercavgscorehs = xtile(avgscores_hs) , n(3)
gen lowavgscorehs = (tercavgscorehs==1)
gen medavgscorehs = (tercavgscorehs==2)
gen highavgscorehs = (tercavgscorehs==3)
	
egen terchp = xtile(Elem_price_residV2_hs) , n(3)
gen lowhsprice = (terchp==1)
gen medhsprice = (terchp==2)
gen highhsprice = (terchp==3)	

xtreg avgscore_diff $outcomecats $lotcons if lunch==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store A

xtreg avgscore_diff $outcomecats $lotcons if lunch==0 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store B

xtreg avgscore_diff $outcomecats $lotcons if male==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store C

xtreg avgscore_diff $outcomecats $lotcons if male==0 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store D

xtreg avgscore_diff $outcomecats $lotcons if white==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store E

xtreg avgscore_diff $outcomecats $lotcons if white==0 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store F

xtreg avgscore_diff $outcomecats $lotcons if lowavgscorehs==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store G

xtreg avgscore_diff $outcomecats $lotcons if medavgscorehs==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store H

xtreg avgscore_diff $outcomecats $lotcons if highavgscorehs==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store I

xtreg avgscore_diff $outcomecats $lotcons if lowhsprice==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store J

xtreg avgscore_diff $outcomecats $lotcons if medhsprice==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store K

xtreg avgscore_diff $outcomecats $lotcons if highhsprice==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store L

** Table A7
esttab A B C D E F G H I J K L using tableA7.tex , replace ///
mtitles("Yes" "No" "Male" "Female" "White" "Non-white" "Low" "Middle" "High" "Low" "Middle" "High") ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" , nolabel ) label booktabs nonum gaps noobs f collabels(none) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS") 

esttab A B C D E F G H I J K L using tableA7.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))




xtreg avgscore_diff $outcomecats $lotcons , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store main

xtreg avgscore_diff $outcomecats $lotcons if appch1magnet==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store A

xtreg avgscore_diff $outcomecats $lotcons if appch1dl==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store B

xtreg avgscore_diff $outcomecats $lotcons if appch1trad==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store C

xtreg avgscore_diff $outcomecats $lotcons if winprob<0.5 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store D

xtreg avgscore_diff $outcomecats $lotcons if winprob>=0.5 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store E

** Table A
esttab main A B C D E using tableA9.tex , replace ///
mtitles("Full Sample" "Magnet" "DL" "Trad." "<0.5" ">=0.5") ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" , nolabel ) label booktabs nonum gaps noobs f collabels(none) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS") 

esttab main A B C D E using tableA9.tex  , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))


/* *********************************************************************************
	********************************* Sixth Graders ********************************
	******************************************************************************** */	
use all-lottery-movers-analysis.dta , clear 

keep if (sixsample==1 & stayer==1)

merge m:1 homeschl using `hsscores' , gen(mergehsscores)

keep if (mergehsscores==1 | mergehsscores==3)

gen mathstand_diff = mathstand_at - mathstand_as
gen readstand_diff = readstand_at - readstand_as
gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as
gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)
gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier
gen move1_win_noncomp = win_noncomplier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1	

global outcomecats "move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp"

egen tercma = xtile(mathstand_hs) , n(3)
gen lowhsma = (tercma==1)
gen medhsma = (tercma==2)
gen highhsma = (tercma==3)

egen tercrd = xtile(readstand_hs) , n(3)
gen lowhsrd = (tercrd==1)
gen medhsrd = (tercrd==2)
gen highhsrd = (tercrd==3)

gen avgscores_hs = (0.5*mathstand_hs + 0.5*readstand_hs)
egen tercavgscorehs = xtile(avgscores_hs) , n(3)
gen lowavgscorehs = (tercavgscorehs==1)
gen medavgscorehs = (tercavgscorehs==2)
gen highavgscorehs = (tercavgscorehs==3)
	
egen terchp = xtile(Mid_price_residV2_hs) , n(3)
gen lowhsprice = (terchp==1)
gen medhsprice = (terchp==2)
gen highhsprice = (terchp==3)	
	
egen tercmath = xtile(math_lag) , n(3)
gen lowmath = (tercmath==1)
gen medmath = (tercmath==2)
gen highmath = (tercmath==3)

egen tercread = xtile(read_lag) , n(3)
gen lowread = (tercread==1)
gen medread = (tercread==2)
gen highread = (tercread==3)

gen avgmathread_lag = (0.5*math_lag + 0.5*read_lag)

egen tercmardavg = xtile(avgmathread_lag) , n(2)
gen lowavg = (tercmardavg==1)
gen highavg = (tercmardavg==2)

xtreg avgscore_diff $outcomecats $lotcons $nccons if lunch==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store A

xtreg avgscore_diff $outcomecats $lotcons $nccons if lunch==0 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store B

xtreg avgscore_diff $outcomecats $lotcons $nccons if male==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store C

xtreg avgscore_diff $outcomecats $lotcons $nccons if male==0 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store D

xtreg avgscore_diff  $outcomecats $lotcons $nccons if white==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store E

xtreg avgscore_diff $outcomecats $lotcons $nccons if white==0 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store F

xtreg avgscore_diff $outcomecats $lotcons $nccons if lowavgscorehs==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store G

xtreg avgscore_diff $outcomecats $lotcons $nccons if medavgscorehs==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store H

xtreg avgscore_diff $outcomecats $lotcons $nccons if highavgscorehs==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store I

xtreg avgscore_diff $outcomecats $lotcons $nccons if lowhsprice==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store J

xtreg avgscore_diff $outcomecats $lotcons $nccons if medhsprice==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store K

xtreg avgscore_diff $outcomecats $lotcons $nccons if highhsprice==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store L

xtreg avgscore_diff $outcomecats $lotcons $nccons if lowavg==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store M

xtreg avgscore_diff $outcomecats $lotcons $nccons if highavg==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store O

** Table A8
esttab A B C D E F G H I J K L M O using tableA8.tex , replace ///
mtitles("Yes" "No" "Male" "Female" "White" "Non-white" "Low" "Middle" "High" "Low" "Middle" "High" "Low" "High") ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" , nolabel ) label booktabs nonum gaps noobs f collabels(none) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS") 

esttab A B C D E F G H I J K L M O using tableA8.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))


xtreg avgscore_diff $outcomecats $lotcons , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store main

xtreg avgscore_diff $outcomecats $lotcons if appch1magnet==0 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store A

xtreg avgscore_diff $outcomecats $lotcons if appch1magnet==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store B

xtreg avgscore_diff $outcomecats $lotcons if appch1ib==1 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store C

xtreg avgscore_diff $outcomecats $lotcons if winprob<0.5 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store D

xtreg avgscore_diff $outcomecats $lotcons if winprob>=0.5 , fe cluster(lottery) 
estadd scalar N_ind = e(N)
est store E

** Table A10
esttab main A B C D E using tableA10.tex , replace ///
mtitles("Full Sample" "Non-Mag." "Magnet" "IB" "<0.5" ">=0.5") ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" , nolabel ) label booktabs nonum gaps noobs f collabels(none) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS") 

esttab main A B C D E using tableA10.tex  , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))










